<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>下载java接口客户端代码</title>
  <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
  <link rel="stylesheet" href="../static/css/dropzone.min.css" th:href="@{/css/dropzone.min.css}">
  <style>
      .main-section {
          margin: 0 auto;
          padding: 20px;
          margin-top: 100px;
          background-color: #fff;
          box-shadow: 0px 0px 20px #c1c1c1;
      }

      .fileinput-remove,
      .fileinput-upload {
          display: none;
      }
  </style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="/">代码生成器</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
          aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="/">Typescript</a>
      </li>
      <li class="nav-item active">
        <a class="nav-link" href="/java">Java <span class="sr-only">(current)</span></a>
      </li>
    </ul>
  </div>
</nav>
<div class="container" style="padding: 40px">
  <h3>用符合openAPI3 规范的json格式接口文档文件下载java接口客户端代码</h3>
  <div style="height: 20px"></div>
  <div class="row">
    <div class="col">
      <div class="card">
        <div class="card-body">
          <form enctype="multipart/form-data" method="POST" th:action="@{/java/upload}">
            <div class="form-group">
              <label for="groupId">groupId</label>
              <input type="text" class="form-control" id="groupId" name="groupId"/>
            </div>
            <div class="form-group">
              <label for="artifactId">artifactId</label>
              <input type="text" class="form-control" id="artifactId" name="artifactId"/>
            </div>
            <div class="form-group">
              <label for="version">version</label>
              <input type="text" class="form-control" id="version" name="version"/>
            </div>

            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="simple-jar" value="simple-jar">
              <label class="form-check-label" for="simple-jar">
                简单jar包，不含内置代码
              </label>
            </div>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="simple-source-jar" value="simple-source-jar">
              <label class="form-check-label" for="simple-source-jar">
                简单源码Jar包，不含内置代码
              </label>
            </div>

            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="simple-fat-jar" value="simple-fat-jar">
              <label class="form-check-label" for="simple-fat-jar">
                含依赖的简单jar包，不含内置代码
              </label>
            </div>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="core-jar" value="core-jar">
              <label class="form-check-label" for="core-jar">
                内置代码的Jar包
              </label>
            </div>

            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="core-source-jar" value="core-source-jar">
              <label class="form-check-label" for="core-source-jar">
                内置代码的源码Jar包
              </label>
            </div>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="core-fat-jar" value="core-fat-jar">
              <label class="form-check-label" for="core-fat-jar">
                含依赖的内置代码Jar包
              </label>
            </div>


            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="full-jar" value="full-jar">
              <label class="form-check-label" for="full-jar">
                完整jar包，含内置代码
              </label>
            </div>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="full-source-jar" value="full-source-jar">
              <label class="form-check-label" for="full-source-jar">
                完整源码Jar包，含内置代码
              </label>
            </div>

            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="full-fat-jar" value="full-fat-jar">
              <label class="form-check-label" for="full-fat-jar">
                含依赖的完整jar包，含内置代码
              </label>
            </div>
            <div class="form-check">
              <input class="form-check-input" type="checkbox" name="packageTypes" id="origin-zip" value="origin-zip">
              <label class="form-check-label" for="origin-zip">
                原生代码压缩文件
              </label>
            </div>

            <div class="dropzone" id="my-dropzone">
              <div class="dz-message">可拖拽上传，亦可选择文件上传<br/>Drop files to upload (or click)</div>
            </div>

          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="../static/js/jquery-3.6.0.min.js" th:src="@{/js/jquery-3.6.0.min.js}" type="text/javascript"></script>
<script src="../static/js/bootstrap.bundle.js" th:src="@{/js/bootstrap.bundle.js}" type="text/javascript"></script>
<script src="../static/js/dropzone.min.js" th:src="@{/js/dropzone.min.js}" type="text/javascript"></script>
<script>
  Dropzone.autoDiscover = false;
  // or disable for specific dropzone:
  // Dropzone.options.myDropzone = false;

  $(function () {
    // Now that the DOM is fully loaded, create the dropzone, and setup the
    // event listeners
    var fileName = "code.zip";

    function hand() {
      var re = /filename="(.+)"/
      var dispo = this.getResponseHeader('Content-Disposition')
      if (re.test(dispo)) {
        fileName = decodeURI(dispo.match(re)[1])
      }
    }

    var myDropzone = new Dropzone("#my-dropzone", { url: "/java/upload", timeout: 600000});
    myDropzone.on("sending", function (file, xhr, formData) {
      xhr.responseType = "blob"
      xhr.onreadystatechange = hand;
      formData.append("groupId", $("#groupId").val());
      formData.append("artifactId", $("#artifactId").val());
      formData.append("version", $("#version").val());
      formData.append("packageTypes", $("input[name=\"packageTypes\"]:checked").map(function(_, el) {
        return $(el).val();
      }).get());
    })
    myDropzone.on("success", function (file) {
      const url = window.URL.createObjectURL(file.xhr.response);
      const link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("id", "downloadLink");
      link.setAttribute("download", fileName);
      document.body.appendChild(link);
      link.click();

      // 删除添加的a链接
      const objLink = document.getElementById("downloadLink");
      document.body.removeChild(objLink);
    });
  })
</script>
</body>
</html>
